<?php

namespace app\common\model;

use think\facade\Db;

class ShopAllianceNoticeModel extends CommonModel{

    protected  $name = 'shop_alliance_notice';

    protected  $pk   = 'id';

    public static function up($data,$where){
        $data['edit_time']=date('Y-m-d H:i:s');
        $res=self::update($data,$where);
        return $res;
    }
    public static function info($where){
        $info = Db::name('shop_alliance_notice')->alias('a')
            ->join('shop b','a.shop_id=b.shop_id')
            ->join('shop_detail d','a.shop_id=d.shop_id')
            ->leftJoin('shop_brand e','b.brand_id=e.id')
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['id']),function ($query) use ($where){
                $query->where(['a.id'=>$where['id']]);
            })
            ->when(isset($where['status']),function ($query) use ($where){
                $query->where(['a.status'=>$where['status']]);
            })
            ->when(!empty($where['shop_name']),function ($query) use ($where){
                $query->where('b.name','like','%'.$where['shop_name'].'%');
            })
            ->when(!empty($where['parent_shop_id']),function ($query) use ($where){
                $query->where(['a.parent_shop_id'=>$where['parent_shop_id']]);
            })
            ->field("a.*,b.name as shop_name,d.logo,d.address,e.name as brand_name,e.id as brand_id")
            ->find();
        if(!empty($info)){
            if(!empty($info['brand_id'])){
                $parent=ShopBrandModel::info(['id'=>$info['parent_id']]);
                if(!empty($parent)){
                    $info['parent_name']=$parent['name'];
                }
            }
        }
        return $info;
    }
    public static function list($where,$order,$page=1,$limit=10){
        $query = Db::name('shop_alliance_notice')->alias('a')
            ->join('shop b','a.shop_id=b.shop_id')
            ->join('shop_detail d','a.shop_id=d.shop_id')
            ->leftJoin('shop_brand e','b.brand_id=e.id')
//            ->leftJoin('shop_brand f','e.parent_id=f.id')
            ->where(['a.status'=>0])
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['shop_name']),function ($query) use ($where){
                $query->where('b.name','like','%'.$where['shop_name'].'%');
            })
            ->when(!empty($where['parent_shop_id']),function ($query) use ($where){
                $query->where(['a.parent_shop_id'=>$where['parent_shop_id']]);
            })
            ->field("a.*,b.name as shop_name,d.logo,d.address,e.name as brand_name,e.id as brand_id");
        $count=$query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        if(!empty($list)){
            foreach ($list as &$v){
                if(!empty($v['brand_id'])){
                    $parent=ShopBrandModel::info(['id'=>$v['parent_id']]);
                    if(!empty($parent)){
                        $v['parent_name']=$parent['name'];
                    }
                }
            }
        }
        return compact('count','list');
    }
}

